//index.js
import { ajax, src, util } from "../../utils/util.js";
//获取应用实例
const app = getApp()

var top = app.globalData.navTop
var height = app.globalData.navHeight

let isIphoneX = app.globalData.isIphoneX;

Page({
  data: {
    userInfo: "",
    isPhoneAuth:0,
    islogin:0,
    isiphone: isIphoneX,
    lxkf:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navHeight: height,
    current: 1,
    navData: [{
      name: "找舱舱", //文本
      index: 0, //是否是当前页，0不是  1是
      style: 0, //样式
      ico: 'faxian', //不同图标
      fn: 'gotoIndex' //对应处理函数
    }, {
      name: "",
      index: "",
      style: 1,
      ico: '',
      fn: 'gotoScan',
    }, {
      name: "我的",
      index: 1,
      style: 0,
      ico: 'wode',
      fn: 'gotoMine'
    },],
    mypetlist:"",
    istoScan:0,
    kftel:""

    
    // hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),

  },
  onLoad: function () {
    var uqid = wx.getStorageSync('uqid');
    console.log(uqid)
    console.log("woshi mine")
    this.setData({
      uqid: uqid
    })
    if (uqid == '') {
      this.setData({
        islogin: 0
      });
    } else {
      this.getInfofirst(uqid);
      this.getkefutel()
    }
  },

  getkefutel: function () {
    ajax("/api/user/service", {
      data: {
        uqid: this.data.uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      console.log(res);
      if (res.error == 0) {
        this.setData({
          kftel: res.data.admin_phone
        })
      }
    })
  },
  onShow:function(){
    var uqid = wx.getStorageSync('uqid');
    var tel = wx.getStorageSync('uqid');
    if (uqid == '' || tel == '') {
      this.setData({
        islogin: 0
      });
    } else {
      this.setData({
        uqid: uqid
      })
      this.getInfofirst(uqid);
    }
  },

  getuserpetlist:function(){
    ajax("/api/user/my_pet", {
      data: {
        uqid: this.data.uqid
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      console.log(res);
      if (res.error == 0) {//获取进展 pid
        this.setData({
          mypetlist:res.data
        })
      }
    })
  },
  //事件处理函数
  gotoScan(e) {
    this.setData({
      istoScan: 1
    })
    if(this.data.islogin){
      this.tonext()
    }else{
      var that = this;
      console.log(e.detail.userInfo)
      if (e.detail.userInfo) {
        //用户按了允许授权按钮
        var code = wx.getStorageSync("code");
        ajax('/api/user/login', {
          data: {
            code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
          method: 'post'
        }, res => {
          console.log(res)
          if (res.error == 0) {
            wx.setStorageSync("uqid", res.data.uqid)

            that.setData({
              uqid: res.data.uqid,
              session_key: res.data.session_key,
              
            });
            this.getInfo(res.data.uqid)
          } else if (res.error == 4003) { //重复授权
            this.getInfo(this.data.uqid)
          }
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })

      }
    }
  },

  //fasong formid
  userSubmit: function (e) {
    if (this.data.uqid) {
      console.log(e.detail.formId);
      ajax('/api/user/form_id', {
        data: {
          uqid: this.data.uqid,
          form_id: e.detail.formId
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'post'
      }, res => {
      })
    }
  },

  //获取个人信息
  getInfo: function (uqid) {
    console.log(uqid)
    ajax("/api/user/user_info", {
      data: {
        uqid: uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res);
      if (res.error == 0) {
        console.log("set 1 次")
        this.setData({
          userInfo: res.data,
         
        });
        if (res.data.phone) {
          console.log(res.data.phone)
          this.setData({
            tel: res.data.phone
          });
          wx.setStorageSync("tel", res.data.phone)
          this.setData({
            islogin:1
          })
        }
        this.isPhone()
        this.getuserpetlist()
      }
    })
  },

  getInfofirst: function (uqid) {
    // console.log(uqid)
    ajax("/api/user/user_info", {
      data: {
        uqid: uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res);
      if (res.error == 0) {
        console.log("set 1 次")
        this.setData({
          userInfo: res.data,
        });
        if (res.data.phone) {
          this.setData({
            tel: res.data.phone
          });
          wx.setStorageSync("tel", res.data.phone)
          this.setData({
            islogin:1
          })
        }
        this.getuserpetlist()
      }
    })
  },

  bindGetUserInfo(e) {
    this.setData({
      istoScan:0
    })
    var that = this;
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var code = wx.getStorageSync("code");
      ajax('/api/user/login', {
        data: {
          code: code,
          nickname: e.detail.userInfo.nickName,
          avatar: e.detail.userInfo.avatarUrl
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'post'
      }, res => {
        console.log(res)
        if (res.error == 0) {
          wx.setStorageSync("uqid", res.data.uqid)

          that.setData({
            uqid: res.data.uqid,
            session_key: res.data.session_key,
           
          });
          this.getInfo(res.data.uqid);
          
        } else if (res.error == 4003) { //重复授权
          this.getInfo(res.data.uqid);
        }
      })
    } else {
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },
  // 是否授权手机号
  isPhone: function () {
    var that = this;
    var tel = wx.getStorageSync("tel");
    console.log(tel)
    if (tel == undefined || tel == '') { //用户没有授权手机号
      that.setData({
        isPhoneAuth: 1
      });
    } else {
      that.setData({
        isPhoneAuth: 0
      })
      // this.getTelAdd();
      console.log(this.data.istoScan)
      if(this.data.istoScan){
        that.tonext()
      }
    }
  },

  tonext: function () {
    var isfirst = this.data.userInfo.is_prompt
    if (isfirst == 1) {
      wx.navigateTo({
        url: '/pages/procedure/procedure',
      })
    } else {
      var _this = this;

      wx.scanCode({
        success: (res) => {
          var result = res.result;
          _this.setData({
            did: result,
          })

          wx.navigateTo({
            url: '/pages/unlocking/unlocking?did=' + this.data.did,
          })
        }
      })
      
    }
  },

  getTelAdd: function () {
    var tel = wx.getStorageSync("tel");
    ajax("/api/user/phone_add", {
      data: {
        uqid: this.data.uqid,
        phone: tel,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'POST'
    }, res => {
      console.log(res);
      if (res.error == 0) {//获取进展 pid
        
        this.getInfo(this.data.uqid);
      }
    })
  },

  getPhoneNumber(e) {
    this.setData({
      isPhoneAuth: 0
    })
    if (e.detail.encryptedData) {
      ajax("/api/user/decrypt_data", {
        data: {
          uqid: this.data.uqid,
          session_key: this.data.session_key,
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'GET'
      }, res => {
        // console.log(res);

        if (res.error == 0) {
          var tel = res.data.phoneNumber;
          wx.setStorageSync("tel", tel)
          this.getInfo(this.data.uqid);
          if (this.data.toScan) {
            that.tonext()
          }
        }
      })

    } 
  },
  refuseUserInfo: function () {
    this.getInfo(this.data.uqid)
    this.setData({
      isPhoneAuth: 0,
    })
    console.log()
    if (this.data.toScan) {
      that.tonext()
    }
  },



  useotherphone: function () {

    this.setData({
      isPhoneAuth: 0
    })

    wx.navigateTo({
      url: '/pages/bindphone/bindphone',
    })
  },




  // 联系客服弹窗
  tuichutanchuang:function(){
    this.setData({
      lxkf: 0
    });
  },
  phoneCall: function (e) {
    var tel = e.currentTarget.dataset.tel;

    if (wx.getStorageSync("uqid")) {
      ajax("/api/user/service", {
        data: {
          uqid: this.data.uqid,
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'get'
      }, res => {
        console.log(res);
        if (res.error == 0) {
          this.setData({
            kftel: res.data.admin_phone
          })
          wx.makePhoneCall({
            phoneNumber: res.data.admin_phone //仅为示例，并非真实的电话号码
          })
        }
      })
    } else {
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }

  },
  lxkffun:function(){
    if(!this.data.islogin){
     if(this.data.uqid!=""){
        this.setData({
          isPhoneAuth: 1
        })
      }else{
       wx.showToast({
         title: '请先授权！',
         icon: 'none',
         duration: 2000
       })
      }
      return
    }
    this.setData({
      lxkf: 1
    });
  },

  tointegral:function(){
    if(!this.data.islogin){
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/integral/integral',
    })
  },
  tomyorder:function(){
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/myorder/myorder',
    })
  },
  tovip: function () {
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/vip/vip',
    })
  },
  tofeedback:function(){
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      
      
      return
    }
    wx.navigateTo({
      url: '/pages/feedback/feedback',
    })
  },
  todianwei:function(){
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/dianwei/dianwei',
    })
  },
  tomsg:function(){
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/msg/msg',
    })
  },
  tohelp:function(){
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/procedure/procedure?nonext=1',
    })
  },
  tomyhongbao:function(){
    if (!this.data.islogin) {
      if (this.data.uqid != "") {
        this.setData({
          isPhoneAuth: 1
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
      return
    }
    wx.navigateTo({
      url: '/pages/coupon/coupon',
    })
  },
  tomydogdetail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/dogdetail/dogdetail?id='+id,
    })
  },
  toadddog: function () {
    wx.navigateTo({
      url: '/pages/adddog/adddog',
    })
  },
  tomyinformation:function(){
    wx.navigateTo({
      url: '/pages/myinformation/myinformation',
    })
  },
  gotoIndex: function (e) {
    console.log(e.currentTarget.dataset.index)
    wx.reLaunch({
      url: '/pages/index/index',
    })
    this.setData({
      current: 0
    });
  },
  gotoMine: function () {
    console.log(e.currentTarget.dataset.index)
    wx.reLaunch({
      url: '/pages/mine/mine',
    })
    this.setData({
      current: 1
    });
  },
  getjifen: function () {
    ajax("/api/index/forward_send", {
      data: {
        uqid: this.data.uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'post'
    }, res => {
      console.log(res);
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.getjifen();
  }
 
})
